import axios from 'axios'
 
// 环境的切换
if (process.env.NODE_ENV === 'development') {
//  axios.defaults.baseURL = 'http://119.23.225.161:9999'
 axios.defaults.baseURL = 'http://192.168.22.225:9999'
} else if (process.env.NODE_ENV === 'server') {
 axios.defaults.baseURL = 'http://119.23.225.161:9999'
//  axios.defaults.baseURL = 'http://119.23.225.161:9999'
}
 
// 请求拦截器
axios.interceptors.request.use(config => {
    config.headers.common = {
        'Accept': "application/json",
        'Content-Type': "application/json",//application/json;charset=UTF-8   application/x-www-form-urlencoded;charset=UTF-8
        'version': "1.0.0"
      }
 return config
 },
 error => {
 return Promise.error(error)
 })
 
axios.defaults.timeout = 10000
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
 
console.log("interceptors");
// 响应拦截器
axios.interceptors.response.use(response => {
 if (response.status === 200) {
 return Promise.resolve(response)
 } else {
 return Promise.reject(response)
 }
}, error => {
 if (error.response.status) {
 // 对不同返回码对相应处理
 return Promise.reject(error.response)
 }
})
 
// get 请求
export function httpGet({
 url,
 params = {}
}) {
 return new Promise((resolve, reject) => {
 axios.get(url, {
  params
 }).then((res) => {
  resolve(res.data)
 }).catch(err => {
  reject(err)
 })
 })
}
 
// post请求
export function httpPost({
 url,
 data = {},
 params = {}
}) {
  console.log(data)

 return new Promise((resolve, reject) => {
 axios({
  url,
  method: 'post',
  // transformRequest: [function (data) {
  // let ret = ''
  // for (let it in data) {
  //  ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  // }
  // return ret
  // }],

  // 发送的数据
  data,
  // url参数
  params
 
 }).then(res => {
  resolve(res.data)
 })
//  .catch(function (error) {
//   console.log("error");
//   console.log(error);
//   console.log("error");
// })
 })
}